clear all
set more off
set maxvar 10000


    file open wgt using "$Mydirectory2/appendix_e/wgt_surveypercohort_stata2tex.tex", write replace
       
        foreach s in anes avtmh57 avtmh76 gss nfs nlsmw nlsom nlsy79 nlsym66 nlsyw68 nsba nsfh ocg62 ocg73 psid  {

    *-------------------------------------------*
    *  STEP 1: DEFINE LOCALS + FIX PSID  
    *-------------------------------------------*
        if "`s'"=="anes" {
            local name "American National Election Survey, 1956--1970"
        }            
        if "`s'"=="avtmh57" {
            local name "Americans View Their Mental Health, 1957"      
             }
        if "`s'"=="avtmh76" {
            local name "Americans View Their Mental Health, 1976"         
              }
        if "`s'"=="gss" {
            local name "General Social Surveys, 1977--2018"          
              }
        if "`s'"=="nfs" {
            local name "National Fertility Study, 1970"        
             }
        if "`s'"=="nlsmw" {
            local name "National Longitudinal Survey of Mature Women, 1967"          
              }
        if "`s'"=="nlsom" {
            local name "National Longitudinal Survey of Older Men, 1966"          
              }
        if "`s'"=="nlsy79" {
            local name "National Longitudinal Survey of Youth, 2002"           
             }
        if "`s'"=="nlsym66" {
            local name "National Longitudinal Survey of Young Men, 1981"         
              }
        if "`s'"=="nlsyw68" {
            local name "National Longitudinal Survey of Young Women, 1988"         
               }
        if "`s'"=="nsba" {
            local name "National Survey of Black Americans, 1979--1980"          
              }
        if "`s'"=="nsfh" {
            local name "National Survey of Families \& Households, 1987--1988"          
              }
        if "`s'"=="ocg62" {
            local name "Occupational Changes in a Generation, 1962"           
             }
        if "`s'"=="ocg73" {
            local name "Occupational Changes in a Generation, 1973"          
              }
        if "`s'"=="psid" {
            local name "Panel Study of Income Dynamics, 1997 \& 2017"             
        }

    *-------------------------------------------------------*
    /*  STEP 2: CALCULATE WEIGHT OF EACH SURVEY WITHIN A 
                DECADE */
    *-------------------------------------------------------*

		use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
         keep if baseline_sample==1

         file write wgt "`name'"    

            foreach d of numlist 1/7 {
                                    
                count if data=="`s'" & decade==19`d'0 //Necessary condition to put dashes in right place in table

                if `r(N)'==0 file write wgt "& ---" 
                    
                else {
                    egen stat_cohortsurvey = sum(wgt_sex_race) if decade==19`d'0 & data=="`s'"
                    sum stat_cohortsurvey if decade==19`d'0 & data=="`s'"
                    local wgt_cohort_survey = `r(mean)'

                    egen stat_cohort = sum(wgt_sex_race) if decade==19`d'0
                    sum stat_cohort if decade==19`d'0 
                    local wgt_cohort = `r(mean)'
                    
                    local share_wsurvcohort = `wgt_cohort_survey' / `wgt_cohort'
                    di "`share_wsurvcohort'"
                    local share2_`d': display %-09.2fc `share_wsurvcohort'

                    file write wgt "& `share2_`d''" 

                    drop stat_cohort*
                }

                if `d'==7 file write wgt "\\" _n

            }
            
        }
    
    file close wgt
